package com.zw.yzk.kabu.management;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseActivity;

/**
 * Created by zhanwei on 2016/12/23.
 */

public class ToolbarBuilder {
    private BaseActivity activity;
    private String title;
    private int textColor;
    private String rightText;
    private int rightImageId;
    private int navigatorId;
    private int bgColor;


    private ToolbarBuilder(BaseActivity activity) {
        this.activity = activity;
        rightImageId = -1;
        //bgColor = ContextCompat.getColor(activity, R.color.black);
        //textColor = ContextCompat.getColor(activity, R.color.white);
        navigatorId = R.drawable.ic_arrow_left_white;
    }

    public static ToolbarBuilder createBuilder(BaseActivity activity) {
        return new ToolbarBuilder(activity);
    }

    public ToolbarBuilder setTitle(String title) {
        this.title = title;
        return this;
    }

    public ToolbarBuilder setRightText(String rightText) {
        this.rightText = rightText;
        return this;
    }

    public ToolbarBuilder setRightImage(int resId) {
        this.rightImageId = resId;
        return this;
    }

    public ToolbarBuilder setNavigationResource(int resId) {
        this.navigatorId = resId;
        return this;
    }

    public ToolbarBuilder setTextColor(int color) {
        this.textColor = color;
        return this;
    }

    public ToolbarBuilder setBackgroundColor(int color) {
        this.bgColor = color;
        return this;
    }

    public View build() {
        View toolbar = activity.findViewById(R.id.navigation_bar);
        if (toolbar == null) {
            return null;
        }
        //设置背景颜色
        if(bgColor != 0) toolbar.setBackgroundColor(bgColor);
        //设置右侧文本
        TextView right = (TextView) toolbar.findViewById(R.id.right_text);
        if (!TextUtils.isEmpty(rightText)) {
            right.setVisibility(View.VISIBLE);
            right.setText(rightText);
        }
        if(textColor !=0) right.setTextColor(textColor);

        //设置右侧图片
        ImageView imageView = (ImageView) toolbar.findViewById(R.id.right_image);
        if (rightImageId != -1) {
            imageView.setVisibility(View.VISIBLE);
            imageView.setImageResource(rightImageId);
        }

        //设置title
        TextView t = (TextView) toolbar.findViewById(R.id.tool_bar_title);
        t.setText(title);
        //设置title文字颜色
        if(textColor !=0) t.setTextColor(textColor);

        //设置navigation背景
        ImageView back = (ImageView) toolbar.findViewById(R.id.back);
        back.setImageResource(navigatorId);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.finish();
            }
        });

        return toolbar;
    }
}
